<?php
/**
 * Created by PhpStorm.
 * User: zsl
 * Date: 2019-09-08
 * Time: 22:39
 */

namespace App\Service;

use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;

class SendEmailService
{
    public function send(string $subject, string $body)
    {
        try {
            $mail = new PHPMailer();
            assert($mail instanceof PHPMailer);
            $mail->isSMTP();                                            // Set mailer to use SMTP
            $mail->Host = 'smtp.qq.com';                          // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                                   // Enable SMTP authentication
            $mail->Username = '1486145487@qq.com';                     // SMTP username
            $mail->Password = 'nivkribdtpwsgcac';                     // SMTP password
            $mail->SMTPSecure = 'ssl';                                  // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 465;                                    // TCP port to connect to

            $mail->setFrom('1486145487@qq.com', 'Mailer');

            $mail->addAddress('1486145487@qq.com', 'Joe User');     // Add a recipient
            $mail->addReplyTo('1486145487@qq.com', 'Information');
            $mail->addCC('1486145487@qq.com');

            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = $subject;
            $mail->Body = $body;

            $mail->send();
            return "发送成功！";
        } catch (Exception $e) {
            return "发送异常！";
        }
    }
}